#!/bin/bash

# 鼎红云屏项目清理脚本
# 用于清理 Minikube 中的部署

set -e

echo "开始清理鼎红云屏项目..."

# 删除 Kubernetes 资源
echo "删除 Kubernetes 资源..."
kubectl delete -f nginx-ingress.yaml --ignore-not-found=true
kubectl delete -f web-deployment.yaml --ignore-not-found=true
kubectl delete -f nginx-config.yaml --ignore-not-found=true
kubectl delete -f server-deployment.yaml --ignore-not-found=true
kubectl delete -f configmap-server.yaml --ignore-not-found=true
kubectl delete -f redis-deployment.yaml --ignore-not-found=true
kubectl delete -f mysql-deployment.yaml --ignore-not-found=true
kubectl delete -f namespace.yaml --ignore-not-found=true

# 等待资源删除完成
echo "等待资源删除完成..."
sleep 10

# 删除本地 Docker 镜像
echo "删除本地 Docker 镜像..."
docker rmi dinghong-cloud-screen-server:latest --force 2>/dev/null || true
docker rmi dinghong-cloud-screen-web:latest --force 2>/dev/null || true

# 从 Minikube 中删除镜像
echo "从 Minikube 中删除镜像..."
minikube image rm dinghong-cloud-screen-server:latest 2>/dev/null || true
minikube image rm dinghong-cloud-screen-web:latest 2>/dev/null || true

# 清理 hosts 文件中的记录
echo "清理 hosts 文件中的记录..."
if grep -q "dinghong-cloud-screen.local" /etc/hosts; then
    sudo sed -i '/dinghong-cloud-screen.local/d' /etc/hosts
    echo "已清理 hosts 记录"
fi

echo "清理完成！" 